<?php

namespace App\Exceptions;

use App\Http\Controllers\API\HeartLogController;
use Exception;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Validation\ValidationException;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * Report or log an exception.
     *
     * @param \Exception $exception
     * @return void
     *
     * @throws \Exception
     */
    public function report(Exception $exception)
    {
        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Exception $exception
     * @return \Symfony\Component\HttpFoundation\Response
     *
     * @throws \Exception
     */
    public function render($request, Exception $exception)
    {
        if ($request->is('api/visit-web-tasks/show') || $request->is('api/visit-web-tasks/create')) {
            //todo status = 200 foramt = [code,msg,data[]]
            if ($exception instanceof ValidationException) {
                return response()->json(['code' => \Symfony\Component\HttpFoundation\Response::HTTP_UNPROCESSABLE_ENTITY, 'message' => '数据无效', 'data' => $exception->validator->getMessageBag()]);
            }
            $error = $this->convertExceptionToResponse($exception);

            $response['code'] = -1;
            $response['message'] = 'unknown errors';
            $response['data'] = ['http_status' => $error->getStatusCode(), 'errors' => $exception->getMessage()];
            return response()->json($response);
        }

        if ($request->is('api/*')) {
            //todo status = 200 foramt = [code,msg,data[]]
            if ($exception instanceof ValidationException) {
                return response()->json(['code' => HeartLogController::DATA_VALIDATE_FAIL_CODE, 'msg' => '数据无效', 'data' => $exception->validator->getMessageBag()]);
            }
            $response = [];
            $error = $this->convertExceptionToResponse($exception);
            $response['msg'] = 'Internal Server Error';
            if (config('app.debug')) {
                $response['msg'] = empty($exception->getMessage()) ? 'Internal Server Error' : $exception->getMessage();
                if ($error->getStatusCode() >= 500) {//500
                    if (config('app.debug')) {
                        $response['trace'] = $exception->getTraceAsString();
                    }
                }
            }
            $response['code'] = HeartLogController::UNKNOWN_ERROR;
            $response['data'] = ['http_status' => $error->getStatusCode(), 'errors' => $exception->getMessage()];
            return response()->json($response);
        }
        return parent::render($request, $exception);
    }
}
